class InvoicesController < ApplicationController
  # GET /invoices
  # GET /invoices.json
  def index
    @invoices = Invoice.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @invoices }
    end
  end

  # GET /invoices/1
  # GET /invoices/1.json
  def show
    @invoice = Invoice.find(params[:id])
    # get Customer 
    @customer = Customer.find(@invoice.customer_id)
    
    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @invoice }
    end
  end

  # GET /invoices/new
  # GET /invoices/new.json
  def new
    @invoice = Invoice.new
    
    3.times do         
      @invoice.invoice_items.build 
    end 
#    invoice_items = @invoice.invoice_items.build
    
    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @invoice }
    end
  end

  # GET /invoices/1/edit
  def edit
    @invoice = Invoice.find(params[:id])
  end

  # POST /invoices
  # POST /invoices.json
  def create
    @invoice = Invoice.new(params[:invoice])
    # insert the user session
    @invoice.user_id = session[:user_id]
    puts "user id: "+ @invoice.user_id.to_s()
      
    @invoice.invoice_items.each do |item|
      style = Inventory.where("product_id=? and size=?",item.product_id,item.size).first      
      if style.nil?
        # product does not exist should output error        
      else   
        remaining = style.quantity - item.quantity
        style.update_attributes(:quantity => remaining  )
      end 
    end
    
    respond_to do |format|
      if @invoice.save          
        format.html { redirect_to @invoice, notice: 'Invoice was successfully created.' }
        format.json { render json: @invoice, status: :created, location: @invoice }
      else
        format.html { render action: "new" }
        format.json { render json: @invoice.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /invoices/1
  # PUT /invoices/1.json
  def update
    @invoice = Invoice.find(params[:id])

    # update the inventory 
    @invoice.invoice_items.each_with_index do |item, index|        
      logger.debug "old quantity : " + item.quantity.to_s()
      logger.debug "params:"+ params[:invoice][:invoice_items_attributes][index.to_s][:quantity]

      difference = item.quantity - params[:invoice][:invoice_items_attributes][index.to_s][:quantity].to_i()

       style = Inventory.where("product_id=? and size=?",item.product_id,item.size).first      
        if style.nil?
          # product does not exist should output error

        else  
          remaining = style.quantity + difference
          style.update_attributes(:quantity => remaining)
        end
    end 
    
    respond_to do |format|
      if @invoice.update_attributes(params[:invoice])
        format.html { redirect_to @invoice, notice: 'Invoice was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @invoice.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /invoices/1
  # DELETE /invoices/1.json
  def destroy
    @invoice = Invoice.find(params[:id])
    @invoice.destroy

    respond_to do |format|
      format.html { redirect_to invoices_url }
      format.json { head :no_content }
    end
  end
  
  def update_inventory
    
  end 

  def get_sizes
    logger.debug "Get options starts..........."
    # @inventories = Inventory.find(:all, :select => "DISTINCT size",:conditions => ["product_id = ?", params[:id]])
    # @inventories = Inventory.where('product_id =?', params[:id]).order('size asc')
    # @inventories = Inventory.order('size asc').where('product_id =?', params[:id]).uniq
    @inventories = Inventory.order('size asc').select(:size).uniq.where('product_id =?', params[:id])
    @sizes = @inventories.collect{ |style| [style.size, style.size] }
    
    # @size_numbers = @inventories.collect {|style| [style.size_number, style.size_number]}
    respond_to do |format|
      format.json { render json: @sizes }
    end
    
  end 
  
  def get_size_numbers
#    @styles = Style.find(:all, :conditions => ["product_id = ?", params[:id]])    
    @styles = Inventory.where('product_id = ? and size = ?', params[:id],params[:size]).order('size_number asc')
    @size_numbers = @styles.collect {|style| [style.size_number, style.size_number]}
    respond_to do |format|
      format.json { render json: @size_numbers }
    end
    
  end

end
